package com.gitee.jjnnzb.designpatterninspring.business.sensitive_word.handler.impl;

import com.gitee.jjnnzb.designpatterninspring.business.sensitive_word.entity.dto.Emits;
import com.gitee.jjnnzb.designpatterninspring.business.sensitive_word.entity.dto.Trie;
import com.gitee.jjnnzb.designpatterninspring.business.sensitive_word.entity.vo.SingleResult;
import com.gitee.jjnnzb.designpatterninspring.business.sensitive_word.handler.SensitiveWordFilter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Jjn
 * @since 2023/8/11 14:58
 */
@Service
@Order(1)
@RequiredArgsConstructor
public class RegionFilterImpl implements SensitiveWordFilter {
    
    private final Trie regionTrie;
    
    @Override
    public void filter(Map<String, Object> map) {
        String content = (String) map.get("content");
        if (StringUtils.isBlank(content)) {
            return;
        }
        @SuppressWarnings("unchecked")
        List<SingleResult> results = (List<SingleResult>) map.get("result");
        Emits emits = regionTrie.findAll(content, false);
        if (CollectionUtils.isEmpty(emits)) {
            return;
        }
        results.add(new SingleResult().setResult(emits).setType("regions"));
    }
}
